package com.niiwoo.civet.account.service.local.openAccount2.listener;

import com.alibaba.fastjson.JSON;
import com.niiwoo.activity.event.constant.BizEventRabbitConstant;
import com.niiwoo.activity.event.dto.request.AccountEventReqDTO;
import com.niiwoo.activity.event.enums.UserRoleTypeEventEnum;
import com.niiwoo.civet.account.constant.MQConstant;
import com.niiwoo.civet.account.dao.entity.BankCard;
import com.niiwoo.civet.account.dao.entity.OpenAccountRecord;
import com.niiwoo.civet.account.dao.mapper.BankCardMapperExt;
import com.niiwoo.civet.account.dto.request.UserIdentityDataMQDTO;
import com.niiwoo.civet.account.enums.AccountTypeEnum;
import com.niiwoo.civet.account.enums.OpenAccountMajorWay;
import com.niiwoo.civet.account.enums.OpenAccountRecordStatusEnum;
import com.niiwoo.civet.account.enums.OpenAccountWayEnum;
import com.niiwoo.civet.account.service.local.openAccount2.dto.OpenAccountEvent;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class TagListener implements OpenAccountListener {


    private final OpenAccountRecordStatusEnum EXCEPT_STATUS = OpenAccountRecordStatusEnum.SUCCESS;

    @Autowired
    BankCardMapperExt bankCardMapperExt;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    PrivacyMasks privacyMasks;

    @Transactional(isolation = Isolation.READ_COMMITTED)
    @Override
    public void handle(OpenAccountEvent event) {
        OpenAccountRecord record = event.getRecord();
        BankCard bankCard = bankCardMapperExt.selectSimpleUserBankCard(record.getUserId());
        // 银行卡账户为空不处理
        if (bankCard == null) {
            log.error("初次开户成功###同步标签，银行卡为空");
            return;
        }
        AccountEventReqDTO json = new AccountEventReqDTO();
        json.setUserId(record.getUserId());
        json.setOpenAccountDate(record.getSuccessTime());
        json.setBankCode(bankCard.getBankCode());
        json.setIdentityCard(bankCard.getIdCard());
        json.setUpdateDate(new Date());
        switch (UserRoleEnum.enumOf(record.getAccountRole())) {
            case BORROWERS: {
                json.setUserRoleTypeEventEnum(UserRoleTypeEventEnum.BORROWERS);
            } break;
            case INVESTOR: {
                json.setUserRoleTypeEventEnum(UserRoleTypeEventEnum.INVESTOR);
            } break;
            case GUARANTEECORP: {
                json.setUserRoleTypeEventEnum(UserRoleTypeEventEnum.GUARANTEECORP);
            } break;
            default: {
                log.error("初次开户成功###同步标签，未知角色={}", record.getAccountRole());
                return;
            }
        }
        log.info("初次开户成功###同步标签, data={}", JSON.toJSONString(json));
        rabbitTemplate.convertAndSend(BizEventRabbitConstant.Exchange.BIZ_EVENT_DIRECT_EXCHANGE, BizEventRabbitConstant.Key.BIZ_EVENT_ACCOUNT_KEY, json);
    }

    @Override
    public boolean isExpectStatus(OpenAccountRecordStatusEnum status) {
        return EXCEPT_STATUS == status;
    }

    @Override
    public boolean isInterestedOpenAccountWay(OpenAccountWayEnum way) {
        return true;
    }

}
